<?php


function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
    return;
}

function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
    return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);
    
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
return;
}




    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=Species.xls ");
    header("Content-Transfer-Encoding: binary ");

    // XLS Data Cell


    if(count($researchs)<=0)
    {
                  
        exit();
    }
                xlsBOF();
                
                    xlsWriteLabel(1,5,"Species Data (".count($researchs).")");
                
                
                
                $xlsRow = 1;
                $xlsNextResearch = "";
                foreach($researchs as $r)
                {
                    if ($xlsNextResearch!=$r->Observationdepth->Observation->Research["id"])
                    {$xlsRow += 3;
                        xlsWriteLabel($xlsRow,0,"Research ID");
                        xlsWriteNumber($xlsRow, 1, $r->Observationdepth->Observation->Research["id"]);
                        $xlsRow++;
                        xlsWriteLabel($xlsRow,0,"Research Title");
                        xlsWriteLabel($xlsRow, 1, $r->Observationdepth->Observation->Research["title"]);
                        $xlsRow++;
                        xlsWriteLabel($xlsRow,0,"Contributor");
                        xlsWriteLabel($xlsRow, 1, $r->Observationdepth->Observation->Research->Entity["firstname"]);
                        $xlsRow++;
                        xlsWriteLabel($xlsRow,0,"Citation");
                       xlsWriteLabel($xlsRow, 1, strip_tags(Structures_BibTex::bibToCite(html_entity_decode($r->Observationdepth->Observation->Research["citation"]))));
                        $xlsRow++;
                        xlsWriteLabel($xlsRow,0,"Remarks");
                       xlsWriteLabel($xlsRow, 1, $r->Observationdepth->Observation->Research["remarks"]);
                        $xlsRow+=2;
                        xlsWriteLabel($xlsRow,0,"Location");
                        xlsWriteLabel($xlsRow,1,"Lat");
                        xlsWriteLabel($xlsRow,2,"Lng");
                        xlsWriteLabel($xlsRow,3,"Observation Date");
                        xlsWriteLabel($xlsRow,4,"Observation End Date");
                        xlsWriteLabel($xlsRow,5,"Observation Time");
                        xlsWriteLabel($xlsRow,6,"Location Remarks");
                        xlsWriteLabel($xlsRow,7,"Depth");
                        xlsWriteLabel($xlsRow,8,"Species");
                        xlsWriteLabel($xlsRow,9,"Quantity");
                        xlsWriteLabel($xlsRow,10,"Tags");
                        $xlsRow++;

                    }
                       xlsWriteLabel($xlsRow, 0, $r->Observationdepth->Observation["location"]);
                       xlsWriteLabel($xlsRow, 1, $r->Observationdepth->Observation["lat"]);
                       xlsWriteLabel($xlsRow, 2, $r->Observationdepth->Observation["lng"]);
                       xlsWriteLabel($xlsRow, 3, $r->Observationdepth->Observation["observationdate"]);
                       xlsWriteLabel($xlsRow, 4, $r->Observationdepth->Observation["observationenddate"]);
                       xlsWriteLabel($xlsRow, 5, $r->Observationdepth->Observation["observationtime"]);
                       xlsWriteLabel($xlsRow, 6, $r->Observationdepth->Observation["remarks"]);
                       xlsWriteLabel($xlsRow, 7, $r->Observationdepth["observationdepth"]."(".$r->Observationdepth->Unit["unit"].")");
                       xlsWriteLabel($xlsRow, 8, $r->Species["species"]);
                       xlsWriteNumber($xlsRow, 9, $r["quantity"]);
                       xlsWriteLabel($xlsRow, 10, $r["tag"]);
                       $xlsNextResearch=$r->Observationdepth->Observation->Research["id"];
                     $xlsRow++;
                }

                     xlsEOF();
                 exit();
?>